// pages/goodsListSearch/goodsListSearch.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    themeColor: app.globalData.themeColor,
    subColor: app.globalData.subColor,
    btnColor: app.globalData.btnColor,
    saleType: 1, //1外卖，2堂食，3服务员点餐,4门店自取
    orderType: 1, //订单类型：1-外卖，2-堂食，3-服务员点餐,4-自取
    goodsList: [],
    goodsName: '',
    vipInfo: {
      discount: 1,
      levelId: 0
    }, //会员信息
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let that = this;
    //订单类型
    if (options.saleType) {
      this.setData({
        saleType: options.saleType,
        orderType: options.orderType,
        'vipInfo.discount': options.discount,
        goodsList: app.globalData.goodsList,
      })
    }
  },

  //菜品输入框改变时
  onGoodsNameChange: function (e) {
    this.setData({
      goodsName: e.detail.value
    })
  },

  //搜索菜品
  seachGoods() {
    let that = this;
    console.log(that.data.goodsName);
    wx.pageScrollTo({
      scrollTop: 0,
      duration: 0,
    })
    let goodsList = [];
    app.globalData.goodsList.forEach((item, index) => {
      if (item.goodsName.indexOf(that.data.goodsName) != -1) {
        goodsList.push(item);
      }
    });
    this.setData({
      goodsList: goodsList
    })
  },

  //前往商品详情页
  goGoodsDetail(e) {
    console.log(e);
    let item = e.currentTarget.dataset.item;

    if (item.stock <= 0) {
      wx.showToast({
        title: '已售罄',
        icon: 'none'
      })
      return;
    }
    if (item.isGroup == 1) {
      //套餐
      this.toSetMealPage({
        currentTarget: {
          dataset: {
            goods: item
          }
        }
      });
    } else {
      wx.redirectTo({
        url: '/pages/goodsDetail/goodsDetail?id=' + item.id + '&saleType=' + this.data.saleType + "&orderType=" + this.data.orderType + "&deskCode=&deskId=",
      })
    }

  },
  //前往套餐页
  toSetMealPage(e) {
    //套餐信息存进全局

    let goods = e.currentTarget.dataset.goods;
    //是否显示vip价格
    let discount = 1;
    if (goods.isShowVipPrice) {
      discount = this.data.vipInfo.discount;
    }
    goods.shopPrice = this.floatPrice(goods.shopPrice * discount);
    app.globalData.setMealInfo = goods;
    wx.redirectTo({
      url: '../../pages/setMeal/setMeal',
    })
  },
  floatPrice(num) {
    if (num) {
      num = parseFloat(num);
      return parseFloat(num.toFixed(2))
    } else return 0;
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})